<% include blocks/header %>
<script>var $user=<%- JSON.stringify($user) %>;</script>
<link rel="stylesheet" href="/libs/css/bootstrap.min.css">
<div id="main" class="container">
    <a class="btn btn-danger btn-lg" record>Stream</a>
    <div>
        <img id="img">
    </div>
    <div id="msg">Monitor to Stream and Save under <span id="selected"></span></div>
requires https or firefox
    <div style="margin-top:30px;">
        <div id="monitors" class="list-group"></div>
    </div>
    <div style="margin-top:30px;" id="data">
    </div>
    <div style="margin-top:30px;">
        <a class="logout btn-danger btn btn-xs">Logout</a>
    </div>
</div>
<video id="video"><source></video>
<canvas id="canvas"></canvas>
<script src="/libs/js/jquery.min.js"></script>
<script src="/libs/js/socket.io.js"></script>
<script src="/libs/js/menu.js"></script>
<script>
$.ccio={};$.ls=localStorage;
$.ccio.ws=io(location.origin);
$.ccio.ws.on('connect',function (d){
    $(document).ready(function(e){
        $.ccio.cx=function(x){if(!x.ke){x.ke=$user.ke;};if(!x.uid){x.uid=$user.uid;};return $.ccio.ws.emit('r',x)}
        console.log($user)
        $.ccio.cx({f:'init',ke:$user.ke,auth:$user.auth_token,uid:$user.uid})
    })
})
$.ccio.ws.on('f',function(d){
    if(d.f!=='monitor_frame'&&d.f!=='cpu'&&d.f!=='event_delete'){console.log(d);}
    switch(d.f){
        case'monitor_edit':
            if(!$.ccio.mon[d.mid]){$.ccio.mon[d.mid]={}}
            $.each(d.mon,function(n,v){
                $.ccio.mon[d.mid][n]=v;
            });
        break;
    }
})
function _base64ToArrayBuffer(base64) {
    var binary_string =  window.atob(base64.replace(/\s/g, "").replace("data:image/jpeg;base64,", ""));
    var len = binary_string.length;
    var bytes = new Uint8Array( len );
    for (var i = 0; i < len; i++)        {
        bytes[i] = binary_string.charCodeAt(i);
    }
    return bytes.buffer;
}
$.ccio.framerate=2;
    
$.ccio.stopSending=function(){
    $('#main').removeClass('recording');
    clearInterval($.ccio.cutChunk);
    $('[record]').unbind('click').click($.ccio.startSending)
}
$.ccio.startSending=function(){
    if($.ccio.selected===null){return false;}
    $('#main').addClass('recording');
	if (typeof MediaRecorder.isTypeSupported == 'function'){
		/*
			MediaRecorder.isTypeSupported is a function announced in https://developers.google.com/web/updates/2016/01/mediarecorder and later introduced in the MediaRecorder API spec http://www.w3.org/TR/mediastream-recording/
		*/
		if (MediaRecorder.isTypeSupported('video/webm;codecs=vp9')) {
		  var options = {mimeType: 'video/webm;codecs=vp9'};
		} else if (MediaRecorder.isTypeSupported('video/webm;codecs=h264')) {
		  var options = {mimeType: 'video/webm;codecs=h264'};
		} else  if (MediaRecorder.isTypeSupported('video/webm;codecs=vp8')) {
		  var options = {mimeType: 'video/webm;codecs=vp8'};
		}
		log('Using '+options.mimeType);
		mediaRecorder = new MediaRecorder($.ccio.vid.stream, options);
	}else{
		log('Using default codecs for browser');
		mediaRecorder = new MediaRecorder($.ccio.vid.stream);
	}

	mediaRecorder.start(10);
    $.ccio.chunker = function(){
        clearInterval($.ccio.cutChunk)
        $.ccio.cutChunk=setInterval(function(){
            mediaRecorder.stop()
            mediaRecorder.start()
        },5000)
    }
	mediaRecorder.ondataavailable = function(e) {
		//log('Data available...');
		//console.log(e.data);
		//console.log(e.data.type);
		//console.log(e);
		chunks.push(e.data);
	};

	mediaRecorder.onerror = function(e){
		clearInterval($.ccio.cutChunk)
	};


	mediaRecorder.onstart = function(){
		$.ccio.chunker()
	};

	mediaRecorder.onstop = function(){
		var blob = new Blob(chunks, {type: "video/webm"});
        $.ccio.cx({f:'monitor_chunk',chunk:blob,mid:$.ccio.selected});
//        var reader = new FileReader();
//        reader.addEventListener("loadend", function() {
//            console.log(reader.result)
//        });
//        reader.readAsArrayBuffer(blob);
		chunks = [];
	};

	mediaRecorder.onpause = function(){
		clearInterval($.ccio.cutChunk)
	}

	mediaRecorder.onresume = function(){
        $.ccio.chunker()
	}

	mediaRecorder.onwarning = function(e){
		clearInterval($.ccio.cutChunk)
	};
    $('[record]').unbind('click').click($.ccio.stopSending)
};
function log(message){
	$.ccio.vid.data[0].innerHTML = $.ccio.vid.data[0].innerHTML+'<br>'+message ;
}

    
/* Webcam */
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
if(getBrowser() == "Chrome"){
	var constraints = {"audio": true, "video": {  "mandatory": {  "minWidth": 640,  "maxWidth": 640, "minHeight": 480,"maxHeight": 480 }, "optional": [] } };//Chrome
}else if(getBrowser() == "Firefox"){
	var constraints = {audio: true,video: {  width: { min: 640, ideal: 640, max: 640 },  height: { min: 480, ideal: 480, max: 480 }}}; //Firefox
}
var mediaRecorder;
var chunks = [];
var count = 0;
    
$.ccio.vid = {element:$('#video')[0],canvas:$('#canvas')[0],data:$('#data')};
$.ccio.vid.element.controls = false;
navigator.getUserMedia(constraints,function(stream,fn) {
    $.ccio.vid.stream=stream;
    $('[record]').click($.ccio.startSending)
}, function(err){console.error('getUserMedia',err)});
    
    
    
    
    
    
    
    
    
    
    //draw selectable mons
    var tmp='';
    if($user.mons&&$user.mons.length>0){
        $.ccio.mon={};
        $.each($user.mons,function(n,v){
            v.details=JSON.parse(v.details);
            $.ccio.mon[v.mid]=v;
            tmp+='<a class="list-group-item" monitor="'+v.mid+'">'+v.name+'</a>';
        })
        $('#monitors').html(tmp)
    }else{
        tmp+="<h2>No Streamer Monitors Setup</h2>"
        tmp+="<small>Login to the Dashboard and add one. Set it to record or watch only.</small>"
        $('#msg').html(tmp)
    }
    delete(tmp);
    $('body').on('click','[monitor]',function(e){
        e.e=$(this);e.a=e.e.attr('monitor'),e.m=$.ccio.mon[e.a];
        $.ccio.selected=e.a;$('#selected').html(e.a);
        if(e.m.details.sfps&&e.m.details.sfps!==''){$.ccio.framerate=e.m.details.sfps}else{$.ccio.framerate=1;}
        $.ls.setItem('Shinobi_Dashcam',e.a)
        $('[monitor]').removeClass('active'),e.e.addClass('active');
    })
    
    
if($user.mons.length>0&&$.ls.getItem('Shinobi_Dashcam')){$('[monitor="'+$.ls.getItem('Shinobi_Dashcam')+'"]').click()}else{$.ccio.selected=null;}
    
$('body')
.on('click','.logout',function(e){
    localStorage.removeItem('ShinobiLogin_'+location.host);location.href='/';
})
    function getBrowser(){
	var nVer = navigator.appVersion;
	var nAgt = navigator.userAgent;
	var browserName  = navigator.appName;
	var fullVersion  = ''+parseFloat(navigator.appVersion);
	var majorVersion = parseInt(navigator.appVersion,10);
	var nameOffset,verOffset,ix;

	// In Opera, the true version is after "Opera" or after "Version"
	if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
	 browserName = "Opera";
	 fullVersion = nAgt.substring(verOffset+6);
	 if ((verOffset=nAgt.indexOf("Version"))!=-1)
	   fullVersion = nAgt.substring(verOffset+8);
	}
	// In MSIE, the true version is after "MSIE" in userAgent
	else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
	 browserName = "Microsoft Internet Explorer";
	 fullVersion = nAgt.substring(verOffset+5);
	}
	// In Chrome, the true version is after "Chrome"
	else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
	 browserName = "Chrome";
	 fullVersion = nAgt.substring(verOffset+7);
	}
	// In Safari, the true version is after "Safari" or after "Version"
	else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
	 browserName = "Safari";
	 fullVersion = nAgt.substring(verOffset+7);
	 if ((verOffset=nAgt.indexOf("Version"))!=-1)
	   fullVersion = nAgt.substring(verOffset+8);
	}
	// In Firefox, the true version is after "Firefox"
	else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
	 browserName = "Firefox";
	 fullVersion = nAgt.substring(verOffset+8);
	}
	// In most other browsers, "name/version" is at the end of userAgent
	else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) <
		   (verOffset=nAgt.lastIndexOf('/')) )
	{
	 browserName = nAgt.substring(nameOffset,verOffset);
	 fullVersion = nAgt.substring(verOffset+1);
	 if (browserName.toLowerCase()==browserName.toUpperCase()) {
	  browserName = navigator.appName;
	 }
	}
	// trim the fullVersion string at semicolon/space if present
	if ((ix=fullVersion.indexOf(";"))!=-1)
	   fullVersion=fullVersion.substring(0,ix);
	if ((ix=fullVersion.indexOf(" "))!=-1)
	   fullVersion=fullVersion.substring(0,ix);

	majorVersion = parseInt(''+fullVersion,10);
	if (isNaN(majorVersion)) {
	 fullVersion  = ''+parseFloat(navigator.appVersion);
	 majorVersion = parseInt(navigator.appVersion,10);
	}


	return browserName;
}
</script>
<style>
    a{cursor:pointer}
    body{color:#fff;background:#333;padding-top:30px;position:relative}
    .recording [record].btn{color: #fff;background-color: #5cb85c;border-color: #4cae4c;}
    #monitors{max-width:250px;width:100%;display:inline-block}
    #main{text-align:center}
    #msg h2{margin-top:0}
    #img{border-radius:5px;overflow:hidden;max-width:100%;display:inline-block;margin-top: 30px}
    #video,#canvas{position: absolute;display:none}
</style>
